package com.lengyu.iottransfer.gateway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lengyu.iottransfer.gateway.entity.Permission;
import com.lengyu.iottransfer.gateway.entity.RolePermissionRelation;
import com.lengyu.iottransfer.gateway.mapper.PermissionMapper;
import com.lengyu.iottransfer.gateway.service.IPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lengyu.iottransfer.gateway.service.IRolePermissionRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuyu
 * @since 2023-01-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private IRolePermissionRelationService rolePermissionService;

    @Override
    public List<Permission> getByRoleId(String roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("role_id",roleId);

        List<Permission> permissionList = new ArrayList<>();
        List<RolePermissionRelation> rolePermissionRelationList = rolePermissionService.list(queryWrapper);
        if(rolePermissionRelationList!= null && rolePermissionRelationList.size() > 0){
            for (int i=0;i<rolePermissionRelationList.size();i++){
                String permissionId = rolePermissionRelationList.get(i).getPermissionId();
                Permission permission = getById(permissionId);
                if(permission != null){
                    permissionList.add(permission);
                }
            }
        }
        return permissionList;
    }
}
